SuperStrict

Rem
	bbdoc: Type description
end rem

Import "../base/ScintillaAbstractDriver.bmx"

'#region Imports
Import brl.System
Import MaxGUI.MaxGUI
Import "-framework Scintilla"
Import "../scintilla.mm"

Extern
	Function SCNScintillaViewAlloc:Int() = "_Z21SCNScintillaViewAlloc"
	Function SCNSendMessage:Int(obj:Int, message:Int, wParam:Int = 0, lParam:Int = 0) = "_Z14SCNSendMessageP13ScintillaViewiii"
	Function SCNSendMessageCharArray:Int(obj:Int, message:Int, wParam:Int = 0, lParam:Byte[]) = "_Z23SCNSendMessageCharArrayP13BBScintillaViewiiP7BBArray"
	Function RegisterNotification(obj:Int, f(a:Int, b:Int, c:Int, d:Int)) 
	Function GenerateScintillaView:Int(obj:Int) 
	Function SendEditorW(obj:Int, message:Int, wParam:Int = 0, lParam:Int = 0) 
End Extern

Function PostScintillaGuiEvent:Int(id:Int,handle:Int,data:Int,mods:Int,x:Int,y:Int,extra:Object )
	Local view:ScintillaMacDriver = ScintillaMacDriver(ScintillaMacDriver.Map.ValueForKey(String(handle)))
	If view <> Null Then
		Print "MY Event"
		view.ReceiveEvent(id,data,mods,x,y,extra)
	End If
End Function


'#end region 

Const WM_COMMAND:Int = 1001
Const WM_NOTIFY:Int = 1002

Type ScintillaMacDriver Extends ScintillaAbstractDriver
	Global Map:TMap = CreateMap()
	
	Field _parent:TGadget
	Field _nsView:Int
	Field _scintilla:Int	
	Field State:Int = False

	Method Init(panel:TGadget)
		_parent = panel
		_nsView = QueryGadget(_parent,QUERY_NSVIEW_CLIENT)
		_scintilla = GenerateScintillaView(_nsView)
		ScintillaMacDriver.Map.Insert(String(_scintilla),Self)
		RegisterNotification(_scintilla,Callback)
	End Method
	
	Function Callback(a:Int,b:Int,c:Int,d:Int)
			Local view:ScintillaMacDriver = ScintillaMacDriver(Map.ValueForKey(String(a)))
			If view <> Null Then
				view.OnNotify(c, d)
			End If
	End Function
	
	Method IsNullDriver:TGadget()
		Return Null
	End Method
	
	Method SendEditor:Int(MSG:Int, wParam:Int = 0, lParam:Int = 0)
		Return SCNSendMessage(_scintilla , msg , wparam , lParam)
	End Method
	
	Method ReceiveEvent(id:Int,data:Int,mods:Int,x:Int,y:Int,extra:Object)
			.PostGuiEvent id,_parent,data,mods,x,y,extra
	End Method
	
	Method DoPrint()
	End Method
End Type

Function CreateDriver:ScintillaAbstractDriver()
	Return New ScintillaMacDriver
End Function



